home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUT14.ZIP / TUT14.TXT < prev    next >
Text File  |  1994-09-08  |  14KB  |  338 lines

  1.                    ╒═══════════════════════════════╕
  2.                    │         W E L C O M E         │
  3.                    │  To the VGA Trainer Program   │ │
  4.                    │              By               │ │
  5.                    │      DENTHOR of ASPHYXIA      │ │ │
  6.                    ╘═══════════════════════════════╛ │ │
  7.                      ────────────────────────────────┘ │
  8.                        ────────────────────────────────┘
  9.  
  10.                            --==[ PART 14 ]==--
  11.  
  12.  
  13.  
  14. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  15. ■ Introduction
  16.  
  17. Hello there. Exams are just around the corner (again :( ), so I thought 
  18. I better get round to doing the next trainer. As usual, there seems to 
  19. have been a big delay between this one and the last one... sorry about 
  20. that ;-)
  21.  
  22. Well, this trainer is mainly on four things : Glenzing, faster polys,
  23. fixed point and assembler. The sample program is basically tut 9
  24. rewritten to include the above.
  25.  
  26. I'll go through them in order, and hopefully you won't have any hassles
  27. grasping the concepts. By the way, do any of you read the text files? I
  28. find myself answering questions via E-Mail etc. that were discussed in
  29. the text sections of the trainers ... oh well, I'll just ramble along
  30. anyway ;-)
  31.  
  32. Please dont send any mail to smith9@batis.bis.und.ac.za anymore ... I 
  33. don't know for how much longer the account will be valid (How can a 
  34. non-BIS person get onto the BIS UNIX machine in the BIS2 directory? If 
  35. his name is Denthor I suppose ;-) Oh well, I got about 8 months use out 
  36. of it. The account expires on Christmas day anyway...) So anyway, please
  37. leave all messages to denthor@beastie.cs.und.ac.za
  38.                                                   
  39.  
  40. If you would like to contact me, or the team, there are many ways you
  41. can do it : 1) Write a message to Grant Smith/Denthor/Asphyxia in private mail
  42.                   on the ASPHYXIA BBS.
  43.             2) Write to :  Grant Smith
  44.                            P.O.Box 270 Kloof
  45.                            3640
  46.                            Natal
  47.                            South Africa
  48.             3) Call me (Grant Smith) at (031) 73 2129 (leave a message if you
  49.                   call during varsity). Call +27-31-73-2129 if you call
  50.                   from outside South Africa. (It's YOUR phone bill ;-))
  51.             4) Write to denthor@beastie.cs.und.ac.za in E-Mail.
  52.             5) Write to asphyxia@beastie.cs.und.ac.za to get to all of
  53.                us at once.
  54.  
  55. NB : If you are a representative of a company or BBS, and want ASPHYXIA
  56.        to do you a demo, leave mail to me; we can discuss it.
  57. NNB : If you have done/attempted a demo, SEND IT TO ME! We are feeling
  58.         quite lonely and want to meet/help out/exchange code with other demo
  59.         groups. What do you have to lose? Leave a message here and we can work
  60.         out how to transfer it. We really want to hear from you!
  61.  
  62.  
  63.  
  64. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  65. ■ What is glenzing?
  66.  
  67. This is an easy one. Imagine, in a 3D object, that all the sides are
  68. made out of colored glass. That means that every time you look through
  69. that side, everything behind it is tinged in a certain color.
  70.  
  71. In ascii ...
  72.           +---------+
  73.           |      <--|---Light blue
  74.           |         |
  75.      +--------+     |
  76.      |    | <-|-----|---Dark blue
  77.      |    +---|-----+
  78.      |     <--|---------Light blue
  79.      +--------+
  80.  
  81. So where the two sides overlap, the color values of the two sides are 
  82. added. Easy huh? It is also easy to code. This is how you do it :
  83.  
  84. Set up your pallette to be a nice run of colors.
  85. Draw your first poly.
  86. While drawing poly 1, instead of plonking down a set pixel color, grab the
  87.   backgrond pixel, add 1 to it, then put the result down.
  88. Draw your second poly.
  89. While drawing poly 2, instead of plonking down a set pixel color, grab the
  90.   backgrond pixel, add 2 to it, then put the result down.
  91. and so forth.
  92.  
  93. So if the color behind poly 1 was 5, you would place pixel 6 down
  94. instead.
  95.  
  96. If you do this for every single pixel of every single side of your 3d 
  97. object, you then have glenzing going. This is obviously slightly slower
  98. then just drawing an item straight, but in the sample program it goes
  99. quite quickly ... this is because of the following sections...
  100.   
  101.  
  102. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  103. ■ Faster Polygons
  104.  
  105. In Tut 9, you probably noticed that we were using a multiply for every
  106. single line of the poly that we drew. This is not good. Let's find out
  107. how to speed it up, shall we...
  108.  
  109. With the multiply method, we went through every line, to find out the
  110. minimum x and maximum x value for that line.
  111.  
  112.                            +
  113.                    ------/---\------- Find min x and max x, draw a line
  114.                        /       \        between them.
  115.                      +           +
  116.                        \       /
  117.                          \   /
  118.                            +
  119.  
  120. How about if we found out all the min and max x's for every line first, 
  121. then just went through an array drawing them. We could do it by 
  122. "scanning" each side in turn. Here is how we do it :
  123.  
  124.                           + 1
  125.                         /
  126.                       /
  127.                   2 +
  128.  
  129. We go from point one to point two. For every single y we go down, we 
  130. move a constant x value. This value is found like this :
  131.  
  132.            xchange := (x1-x2)/(y1-y2)
  133.  
  134. Remember gradients? This is how you calulated the slope of a line waaay
  135. back in school. You never thought it would be any use, didn't you ;-)
  136.  
  137. Anyway, with this value, we can do the following :
  138.  
  139.     For loop1:=y1 to y2 do BEGIN
  140.       [ Put clever stuff here ]
  141.       x:=x+xchange;
  142.     END;
  143.     
  144. and we will go through all the x-values we need for that line. Clever, 
  145. huh?
  146.  
  147. Now for the clever bit. You have an array, from 0 to 199 (which is all
  148. the possible y-values your onscreen poly can have). Inside this is two 
  149. values, which will be your min x and your max x. You start off with the 
  150. min x being a huge number, and the max x being a low number. Then you 
  151. scan a side. For each y, check to see if one of the following has 
  152. happened :    If the x value is smaller then the xmin value in your
  153.                 array, make the xmin value equal to the x value
  154.               If the x value is larger then the xmax value in your
  155.                 array, make the xmax value equal to the x value
  156.  
  157. The loop now looks like this :
  158.  
  159.     For loop1:=y1 to y2 do BEGIN
  160.       if x>poly[loop1,1] then poly[loop1,1]:=x;
  161.       if x<poly[loop1,1] then poly[loop1,1]:=x;
  162.       x:=x+xchange;
  163.     END;
  164.  
  165. Easy? Do this for all four sides (you can change this for polys with 
  166. different numbers of sides), and then you have all the x min and x max
  167. values you need to draw your polygon.
  168.  
  169. In the sample program, if you replaced the Hline procedure with one that
  170. draws solid lines, you could use the given drawpoly for solids.
  171.  
  172. Even this procedure is sped up by the next section, on fixed point.
  173.    
  174.  
  175. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  176. ■  What is fixed point?
  177.  
  178. Have you ever noticed how slow reals are? I mean slooooow. You can get a
  179. massive speed increase in most programs by replacing your reals with 
  180. integers, words etc. But, I hear you cry, what happens to the much 
  181. needed fraction bit after the decimal point? The answer? You keep it. 
  182. Here's how.
  183.  
  184. Let us say you have a word, which is 16 bits. If you want to use it as a
  185. fixed point value, you can separate it into 2 sections, one of which 
  186. holds the whole value, and one which holds the fraction.
  187.  
  188.              00000000  00000000   <-Bits
  189.              Whole     Fraction
  190.              
  191. The number 6.5 would therefore be shown as follows :
  192.  
  193.              Top byte    :  6
  194.              Bottom byte :  128
  195.              
  196. 128 is half (or .5) of 256, and in the case of the fraction section, 256
  197. would equal one whole number.
  198.  
  199. So let us say we had 6.5 * 2. Using reals this would be a slow mul, but 
  200. with fixed point ...
  201.  
  202.             Top Byte    :  6     
  203.             Bottom Byte :  128
  204.             Value       :  1664   <-This is the true value of the word
  205.                                      ie. (top byte*256)+bottom byte).
  206.                                      this is how the computer sees the 
  207.                                      word.
  208.              1664 shl 1 = 3328    <-shl 1 is the same as *2, just faster.
  209.             Top byte    :  13
  210.             Bottom byte :  0
  211.  
  212. As you can see, we got the correct result! And in a fraction of the time
  213. that a multiplication of a real would have taken us. You can add and
  214. subtract fixed point values with no hassles, and multiply and divide 
  215. them by normal values too. When you need the whole value section, you 
  216. can just read the high byte, or do the following
  217.  
  218.              whole = word shr 8
  219.          eg  1664 shr 8 = 6
  220.          
  221. As you can see, the fraction is truncated. Obviously, the more bits you 
  222. set aside for the fraction section, the more accurate your calculation 
  223. is, but the lesser the maximum whole number you can have. For example, 
  224. in the above numbers, the maximum value of your whole number was 256, a
  225. far cry from the 65535 a normal (non fixed point) word's maximum.
  226.  
  227. There are a lot of hassles using fixed point (go on, try shift a
  228. negative value), most of which have to do with the fact that you have 
  229. severely decreased the maximum number you may have, but trust me, the 
  230. speed increase is worth it (With longintegers, and/or extended 386
  231. registers, you can even have 16x16 fixed point, which means high
  232. accuracy and high maximum values)
  233.  
  234. Try write a program using fixed point. It is not difficult and you will
  235. get it perfect easily. Trust me, I'm a democoder ;-)
  236.  
  237. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  238. ■ Assembler
  239.  
  240. In the sample program I used one or two assembler commands that I havent
  241. discussed with you ... here they are ...
  242.  
  243.   imul  value      This is the same as mul, but for integer values. It
  244.                      multiplies ax by the value. If the value is a word,
  245.                      it returns the result in DX:AX
  246.  
  247.   sal  register,value   This is the same as shl, but it is arithmetic,
  248.                           in other words it works on integers. If you
  249.                           had to shl a negative value, the result would
  250.                           mean nothing to you.
  251.  
  252.   rcl  register,value   This is the same as shl, but after you have
  253.                           shifted, the value in the carry flag is placed
  254.                           in the now-vacated rightmost bit. The carry
  255.                           flag is set when you do an operation where the
  256.                           result is greater then the upmost possible
  257.                           value of the variable (usually 65535 or 32767)
  258.                           eg mov ax,64000
  259.                              shl ax,1     {<- Carry flag now = 1}
  260.                              
  261. For more info on shifting etc, re-read tut 7, it goes into the concept
  262. in detail.
  263.  
  264. The sample program is basically Tut 9 rewritten. To see how the
  265. assembler stuff is working, do the following ... Go into 50 line mode
  266. (-Much- easier to debug), then hit [Alt - D] then R. A little box with
  267. all your registers, segments etc and their values will pop up. Move it
  268. down to where you want it, then go back to the program screen (Hit Alt
  269. and it's number together), and resize it so that you have both it and
  270. the register box onscreen at once (Alt - 5 to resize) ... then use F4,
  271. F7 and F8 to trace though the program (you know how). The current value
  272. of the registers will always be in that box.
  273.  
  274.  
  275. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  276. ■ In closing
  277.  
  278. Well, that is about it. The sample program may start as being a little 
  279. intimidating to some when they first look at it, just remember to read 
  280. it with tut 9, very little is different, it's just with fixed point and 
  281. a bit of assembler.
  282.  
  283. Before I forget, with Tut 13, the program crashes if you have error 
  284. checking on. This is how you sort it out :
  285.  
  286. 1) Turn off error checking     or
  287. 2) Make logo a pointer, and get and free the memory     or
  288. 3) Read the logo directly to screen     or
  289. 4) Use the {$M ......} command with various values at the top of the 
  290.      program till it works.
  291.      
  292. I prefer options 3 or 2, but hey ... the problem was that the logo was
  293. rather large (16k), and Pascal likes complaining ;-)
  294.  
  295. I am in doubt as to weather to continue doing quotes ... here is a 
  296. conversation I had with Pipsy, after the group conversation got around 
  297. to weather we were normal or not ...
  298.  
  299. Me : I'm normal.
  300. Pipsy : No your not.
  301. Me : Prove it.
  302. Pipsy : Just look at your quotes in your trainers.
  303. Me : What? You think those are weird?
  304. Pipsy : Too weird.
  305. Me : You mean that there is a weirdness line, and I crossed it?
  306. Pipsy : Yes.
  307.  
  308. Bit of a conversation killer that, so we stopped there.
  309.  
  310. Anyway, this trainer won't have a quote in it ... how about a disclaimer
  311. instead? Feel free to use it in your messages ...
  312.  
  313. ------------------------------------------------------------------------
  314. The views expressed above are mine and not Novells. In fact, I've never
  315. worked for them in my life!
  316.  
  317. Byeeee....
  318.   - Denthor
  319.       18:57
  320.         9-9-94
  321.  
  322.  
  323. The following are official ASPHYXIA distribution sites :
  324.  
  325. ╔══════════════════════════╦════════════════╦═════╗
  326. ║BBS Name                  ║Telephone No.   ║Open ║
  327. ╠══════════════════════════╬════════════════╬═════╣
  328. ║ASPHYXIA BBS #1           ║+27-31-765-5312 ║ALL  ║
  329. ║ASPHYXIA BBS #2           ║+27-31-765-6293 ║ALL  ║
  330. ║C-Spam BBS                ║410-531-5886    ║ALL  ║
  331. ║POP!                      ║+27-12-661-1257 ║ALL  ║
  332. ║Soul Asylum               ║+358-0-5055041  ║ALL  ║
  333. ║Wasted Image              ║407-838-4525    ║ALL  ║
  334. ╚══════════════════════════╩════════════════╩═════╝
  335.  
  336. Leave me mail if you want to become an official Asphyxia BBS
  337. distribution site.
  338.